<?php
class mg_menu extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('mmg_menu');
		$this->load->model('mset_menu');
	}
	
	function index()
	{
		$token = $this->session->userdata('token');
		if($token){
			$data['results'] = $this->mmg_menu->getList();
			$this->load->view('mg_menu/list',$data);
		}else{
			redirect('admin');
		}
	}
    
	function add(){
		$data['mode'] = "Create";
		$data['action'] = "save";
		$data['set'] = $this->mset_menu->getList();
		$this->load->view('mg_menu/form',$data);
	}
	
	function edit($id_menu){
		$data['mode'] = "Edit";
		$data['action'] = "update";
		$data['detail'] = $this->mmg_menu->detail($id_menu);
		$data['set'] = $this->mset_menu->getList();
		$this->load->view('mg_menu/form',$data);
	}
    
	function save()
	{
		$id_menu = $this->input->post('id_menu');
		$title_menu = $this->input->post('title_menu');
		$content_menu = $this->input->post('content_menu');
		$submit = $this->input->post('submit');	
		if ($submit)
		{
			$this->mmg_menu->setData($id_menu,$title_menu,$content_menu);
			$this->mmg_menu->create();
			$this->session->set_flashdata('success', true);
			redirect('mg_menu');
		}
	}
	
	function update()
	{
		$id_menu = $this->input->post('id_menu');
		$id_menu_tmp = $this->input->post('id_menu_tmp');
		$title_menu = $this->input->post('title_menu');
		$content_menu = $this->input->post('content_menu');
		$submit = $this->input->post('submit');	
		if ($submit)
		{
			$this->mmg_menu->setData($id_menu,$title_menu,$content_menu);
			$this->mmg_menu->update($id_menu_tmp);
			$this->session->set_flashdata('success', true);
			redirect('mg_menu');
		}
	}
	
	function delete($id_menu)
	{
		if ($this->mmg_menu->remove($id_menu)){
			$this->session->set_flashdata('delete', true);
			redirect('mg_menu','refresh');
		}	
	}	
}
?>